<?php
if (!defined("OK") || (defined("LEVEL") && LEVEL < 1)) { header('location: ?'); exit; }
if ($_SERVER['PHP_SELF'] == 'codebin.php') { header('location: ../'); exit; }
//require_once('rate/_drawrating.php');
?>
<link rel="stylesheet" type="text/css" href="rate/css/rating.css" />
<?php


# Duomenu paieska
# SELECT `pav` FROM `codebin` WHERE `pav` LIKE CONVERT(_utf8 '%te%' USING latin1) COLLATE latin1_swedish_ci

// Nustatom kintamuosius
if (isset($url['a']) && isnum($url['a']) && $url['a'] > 0) { $aid = ceil((int)$url['a']); }	//
if (isset($url['c']) && isnum($url['c']) && $url['c'] > 0) { $cid = ceil((int)$url['c']); }	//
if (isset($url['d']) && isnum($url['d']) && $url['d'] > 0) { $did = ceil((int)$url['d']); }	//trinamo kodo ID
if (isset($url['p']) && isnum($url['p']) && $url['p'] > 0) { $p = ceil((int)$url['p']); } else { $p = 0; }	//Puslapis
if (isset($url['o']) && isnum($url['o']) && $url['o'] > 0) {
	if ($url['o'] == 0) { $order = "id DESC"; }
	elseif ($url['o'] == 1) { $order = "nick"; }
	elseif ($url['o'] == 2) { $order = "pav"; }
	elseif ($url['o'] == 3) { $order = "data DESC"; }
	elseif ($url['o'] == 4) { $order = "cat"; }
}
else { $order = "data DESC"; }

//Puslapiavimui
$viso = kiek("codebin");
$limit = 50;	//po kiek elementu rodysim

// Naujo kodo iterpimo forma autohide
$code = '
<form name="code" action="?id,7" method="post">
	<input value="" type="input" name="title"/>
	<optgroup label="tipas">
	<label><input type="radio" name="cat" value="PHP" checked="checked" class="middle" /> PHP</label>
	<label><input type="radio" name="cat" value="mIRC" class="middle" /> mIRC</label>
	</optgroup>
	<textarea name="code" id="code" rows="10" style="width: 95%;" wrap="physical"></textarea><br/>
	<input value="Siųsti" type="submit"/>
	<input name="action" value="send" type="hidden"/>
</form>';
if (PRISIJUNGES) { hide("Naujas kodas",$code,((isset($cid) && $cid>0)?true:false),"Naujas kodas"); }

// Rodom koda
if (isset($cid) && $cid != 0) {
	$row = $db->masyvas($db->uzklausa("SELECT * FROM `codebin` WHERE id=".$db->fix($cid)." LIMIT 1"));
	$type = $row['cat'];
	$title = $row['nick']." (".$row['data'].") :: ".input($row['pav']);
	$search = array("[php]","[/php]","[b]","[/b]","[mirc]","[/mirc]");
	$replace = array("","","","","","");
	//$code = "<textarea name=\"code\" class=\"".strtoupper($type)."\" rows=\"15\" cols=\"100\">".input($row['cod'])."</textarea>".rating_bar('Kodas_'.$row['id'],5);
	$code = "<textarea name=\"code\" class=\"".strtoupper($type)."\" rows=\"15\" cols=\"100\">".input($row['cod'])."</textarea>";
	if (!empty($row['cod'])) { hide($title,$code); }
	else { klaida("Klaida","Kodas nerastas arba buvo ištrintas"); redirect("?id,7;p,$p","meta"); }
}

//Iterpiam nauja koda
if (isset($_POST) && !empty($_POST) && isset($_POST['cat']) && isset($_POST['code']) && !empty($_POST['code']) && isset($_POST['action']) && $_POST['action'] == 'send' && isset($_POST['title']) && defined("LEVEL") && LEVEL >= 1) {
	if (isset($_SESSION['username'])) { $uzeris = $_SESSION['username']; } else { $uzeris = "Svečias"; }
	if (isset($_SESSION['id'])) { $uzer_id = $_SESSION['id']; } else { $uzer_id = 0; }
	$db->uzklausa("INSERT INTO `codebin` (`nick`, `nick_id`, `pav`, `cod`, `cat`, `data`) VALUES (".escape($uzeris).", ".escape($uzer_id).", ".escape(htmlspecialchars(strip_tags($_POST['title']))).", ".escape($_POST['code']).", ".escape($_POST['cat'])." , NOW( ))");
	if ($db->rows() > 0) { msg("Informacija","Naujas kodas buvo sėkmingai patalpintas"); redirect("?id,7;c,".mysql_insert_id()."","meta"); } else { klaida("Klaida","Jūsų kodas nebuvo patalpintas. <br>Patikrinkite ar teisingai užpildėte formą"); redirect("?id,7;p,$p","meta"); }
	unset($uzeris,$uzer_id);
}

//Trinam koda
if (isset($did) && $did != 0 && defined("LEVEL") && LEVEL > 20 && !isset($cid)) {
	$db->uzklausa("DELETE FROM `codebin` WHERE `id` = ".$db->fix($did)." LIMIT 1");
	if ($db->rows() > 0) { msg("Kodas ištrintas","Kodas <b>$did</b> sėkmingai ištrintas."); header("Location: ?id,7;p,$p"); } else { klaida("Klaida","Nurodytas kodas nebuvo ištrintas. Prašome patikrinti ar įvesti duomenys buvo teisingi"); redirect("?id,7;p,$p","meta"); }
	$db->uzklausa("DELETE FROM kom WHERE pid=7 AND kid=".$db->fix($did)."");
	$db->uzklausa("DELETE FROM rating WHERE id=".$db->fix("Kodas_".$id)."");
}

// Rodom komentarus
if (isset($cid) && $cid > 0) {
	echo "<hr/>";
	include_once("priedai/komentarai.php");
	komentarai($cid);
}

//darom paieska
elseif (isset($url['s']) && !empty($url['s'])) {
	$search = str_replace(" ","%",$url['s']);
	$sql = $db->uzklausa("SELECT `data`,`nick_id`,`nick`,`id`,`cat`,`pav` FROM `codebin` WHERE `pav` LIKE ".$db->fix("%".$search."%")." OR `cod` LIKE ".$db->fix("%".$search."%")." LIMIT 0 , 30");
	msg("Ieškoma frazė:","<b>".input(str_replace("%"," ",$search))."</b><br/>Rasta atikmenų: ".$db->rows($sql));
}

else {
	lentele("Paieška",'<form name="code_search" action="" method="get" onSubmit="return false"><input value="" type="input" name="s"/><input value="Ieškoti" type="submit" onclick="location.href=\'?id,7;s,\'+document.code_search.s.value+\'\';"/></form>');
	$sql = $db->uzklausa("SELECT `data`,`nick_id`,`nick`,`id`,`cat`,`pav` FROM `codebin` ORDER BY $order LIMIT $p,$limit");
}

print_rr($ses->user_data);
// Rodom kodus
if (!isset($cid)) {
	if (isset($sql) && !empty($sql) && $viso > 0) {
		if ($viso > $limit) { lentele("Puslapiai",puslapiai($p,$limit,$viso,10)); }

		while($row = $db->masyvas($sql)) {
			//data  	nick_id  	nick  	id  	cat  	pav
			$test[] = array(
				"Autorius"=>"<a href='?id,47;m,".$row['nick_id']."'>".$row['nick']."</a>",
				"Pavadinimas"=>"<a href='".url("c,".$row['id']."")."'>".((isset($_POST['s'])?str_replace(input($_POST['s']),"<span style='background-color:yellow'>".input($_POST['s'])."</span>",input($row['pav'])):input($row['pav'])))."</a>",
				"Data"=>naujas($row['data']),
				"Kategorija"=>"<img src='images/icons/".($row['cat']=='PHP'?'page_white_php.png':'page_white_code_red.png')."' border='0' alt='".$row['cat']."'/>"
				);
		}
		include(HOME_DIR."variklis/class/forms_inputs.php");
		$bla = new Table();
		lentele("Kodų dėžutė - ".$db->rows($sql),$bla->render($test));
	?>
	<!-- CodeBin'as -->
	<table width="100%" border="0" align="center" cellpadding="3" cellspacing="0">
	<tr class="title">
		<th><a href="<?php echo url("o,0"); ?>">Nr</a>: <?php echo (isset($url['o']) && $url['o'] == 0)?"↓":""; ?></th>
		<th><a href="<?php echo url("o,1"); ?>">Autorius</a>: <?php echo (isset($url['o']) && $url['o'] == 1)?"↓":""; ?></th>
		<th><a href="<?php echo url("o,2"); ?>">Pavadinimas</a>: <?php echo (isset($url['o']) && $url['o'] == 2)?"↓":"";?></th>
		<th><a href="<?php echo url("o,3"); ?>">Reitingas</a>: <?php echo (isset($url['o']) && $url['o'] == 3)?"↓":""; ?></th>
		<th><a href="<?php echo url("o,4"); ?>">Kategorija</a>: <?php echo (isset($url['o']) && $url['o'] == 4)?"↓":""; ?></th>
	</tr>
	<?php while ($row = $db->masyvas($sql)) { ?>
	<tr>
		<td class="sarasas"><?php echo $row['id']; ?> <?php echo (defined("LEVEL") && LEVEL > 20)?'<a href="?id,7;d,'.$row['id'].'" onclick="return confirm(\'Ar tikrai norite trinti?\')"><img src="images/icons/cancel.png" class="middle" border=0 alt="x"/></a>':''; ?> <?php echo naujas($row['data'],$row['nick']); ?></td>
		<td class="sarasas"><?php echo "<a href='?id,47;m,".$row['nick_id']."'>".$row['nick']."</a>"; ?></td>
		<td class="sarasas"><a href="<?php echo url("c,".$row['id'].""); ?>"><?php echo ((isset($_POST['s'])?str_replace(input($_POST['s']),"<span style='background-color:yellow'>".input($_POST['s'])."</span>",input($row['pav'])):input($row['pav']))); ?></a></td>
		<td class="sarasas"><?php //echo rating_bar('Kodas_'.$row['id'],5); ?> </td>
		<td class="sarasas"><img src="images/icons/<?php echo ($row['cat']=='PHP'?'page_white_php.png':'page_white_code_red.png'); ?>" border="0" alt="<?php echo $row['cat']; ?>"/></td>
	</tr>
	<?php }
	 mysql_free_result($sql); ?>
	</table>
	<!-- CodeBino pabaiga -->
	<?php
	if ($viso > $limit) { lentele("Puslapiai",puslapiai($p,$limit,$viso,10)); }
	}
	elseif (isset($_POST['s'])) { klaida("Klaida","Nėra ką parodyti :)"); }
}
?>
